package com.his.struts.action;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.medicine.Medicine;
import com.his.struts.dao.medicine.MedicineImp;
import com.his.struts.form.MedicineForm;
import com.his.struts.util.PageUtil;
import com.his.struts.util.TranCharset;
/**
 * 药物管理类
 * liuyan
 *  Creation date: 06-13-2011 
 */
public class MedicineAction extends DispatchAction {
	Medicine md = new MedicineImp();
	Logger log =Logger.getLogger(MedicineAction.class);
	/**
	 * 查看所有的药物中药信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward showAllMedicine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forward=null;
		String admin = request.getParameter("admin");
		String doc = request.getParameter("doc");
		if(admin == null && doc == null){
			admin = (String)request.getSession().getAttribute("admin");
			request.getSession().removeAttribute("admin");
		}
		String sql = "Medicine";
		ResultSet rs = PageUtil.getPage(sql, 10, request);

		// 如果数据不为空
		if (rs != null) {
			// 封装
			List<MedicineForm> list = md.getMedicineBean(rs);
			// 数据放到request中
			request.setAttribute("allMedicine", list);
			request.setAttribute("fx", 0);
		}
		if(admin != null){
			forward=mapping.findForward("successShowAll");
		}else if(doc != null){
			forward=mapping.findForward("successShowAll_doc");
		}else{
			forward=mapping.findForward("successShowAll_qt");
		}
		return forward;
	}
	
	/**
	 * 模糊查询药物信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward getMedicine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forward=null;
		String admin = request.getParameter("admin");
		String doc = request.getParameter("doc"); 
		String medicinename = request.getParameter("medicinename").trim();
		String encoding = TranCharset.getEncoding(medicinename);
		if("ISO-8859-1".equals(encoding)){
			medicinename = TranCharset.TranEncodeTOUTF_8(medicinename);
		}
		
		String sql = "medicine where medicine like '%"+medicinename+"%'";
		ResultSet rs = PageUtil.getPage(sql, 10, request);

		// 如果数据不为空
		if (rs != null) {
			// 封装
			List<MedicineForm> list = md.getMedicineBean(rs);
			// 数据放到request中
			request.setAttribute("allMedicine", list);
			request.setAttribute("medicinename", medicinename);
		}
		if(admin != null){
			forward=mapping.findForward("successShowAll");
		}else if(doc != null){
			forward=mapping.findForward("successShowAll_doc");
		}else{
			forward=mapping.findForward("successShowAll_qt");
		}
		return forward;
	}
	/**
	 * 删除一种药物
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delMedicine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forward=null;
		String id = request.getParameter("mdid");
		int mdid = Integer.parseInt(id);
		boolean ok = md.delMedicine(mdid);
		if(ok){
			request.setAttribute("delOk", "删除成功！！！");
			request.setAttribute("message", "删除成功");
			request.getSession().setAttribute("admin", "ly");
			forward=mapping.findForward("successDel");
		}else{
			forward=mapping.findForward("fail");
		}
		return forward;
	}
	
	/**
	 *更新一种药物
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @returnupdateMedicine
	 */
	public ActionForward updateMedicine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forward=null;
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
		}
		MedicineForm mdf = (MedicineForm)form;
		boolean ok = md.updateMedicine(mdf);
		if(ok){
			request.setAttribute("delOk", "更新成功！！！");
			request.setAttribute("message", "修改成功");
			forward=mapping.findForward("successUpdate");
			request.getSession().setAttribute("admin", "ly");
		}else{
			forward=mapping.findForward("fail");
		}
		return forward;
	}
	/**
	 *添加一种药物
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @returnupdateMedicine
	 */
	public ActionForward addMedicine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forward=null;
		MedicineForm mdf = (MedicineForm)form;
		boolean ok = md.addMedicine(mdf);
		if(ok){
			request.setAttribute("delOk", "添加成功！！！");
			request.setAttribute("message", "添加成功");
			request.getSession().setAttribute("admin", "ly");
			forward=mapping.findForward("successAdd");
		}else{
			forward=mapping.findForward("fail");
		}
		return forward;
	}
	/**
	 *查看一类药物
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @returnupdateMedicine
	 */
	public ActionForward showtypeMedicine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			ActionForward forward=null;
			String fx = request.getParameter("fx");
			String admin = request.getParameter("admin");
			String doc = request.getParameter("doc"); 
			int type = Integer.parseInt(fx);
			HttpSession session = request.getSession();
			String sql =  "";
			sql = "Medicine where mtype = " + type;
			if(type == 0){
				sql = "Medicine";
			}
			ResultSet rs = PageUtil.getPage(sql, 10, request);

			// 如果数据不为空
			if (rs != null) {
				// 封装
				List<MedicineForm> list = md.getMedicineBean(rs);
				// 数据放到request中
				request.setAttribute("allMedicine", list);
				request.setAttribute("fx", fx);
				request.getSession().setAttribute("allMedicines", list);
			}
			
			if(admin != null){
				forward=mapping.findForward("successShowAll");
			}else if(doc != null){
				forward=mapping.findForward("successShowAll_doc");
			}else{
				forward=mapping.findForward("successShowAll_qt");
			}
		return forward;
	}
}